package com.wy.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@Controller
@RequestMapping("byte")
@CrossOrigin("*")
public class ByteController {
    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestBody byte[] fileBytes) {
        try {
            // 保存字节数组到本地
            String filePath = "C:\\Users\\Administrator\\Desktop\\ffff\\1.pdf";
            Files.write(Paths.get(filePath), fileBytes);
            return ResponseEntity.ok("文件上传成功");
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败");
        }
    }
    @GetMapping("/download")
    public ResponseEntity<byte[]> downloadFile() {
        try {
            // 从本地读取文件为字节数组
            byte[] fileBytes = Files.readAllBytes(Paths.get("C:\\Users\\Administrator\\Desktop\\ffff\\1.zip"));
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"downloaded_file.zip\"")
                    .body(fileBytes);
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }
}
